/*------------------------------------------------------------------------
# SystemServiceImpl.java - VipApi
# ------------------------------------------------------------------------
# 
# 获取基本配置信息接口Service
# 
# author    wlm
# date  	2014/03/19
# version 	v1.0
# copyright Copyright (C) 2014 wevip.com. All Rights Reserved.
# Websites: http://www.wevip.com
# Technical Support:  Forum - http://www.wevip.com
# 
# 1、新规
#
 */
package cn.com.api.service.impl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiToken;
import cn.com.api.entity.Apilog;
import cn.com.api.entity.Version;
import cn.com.api.mapper.SystemMapper;
import cn.com.api.service.SystemService;
import cn.com.response.ApiResult;
import cn.com.response.ApiResultPage;

@Service
public class SystemServiceImpl implements SystemService {

	@Autowired
	private SystemMapper systemMapper;

	public void setSystemMapper(SystemMapper systemMapper) {
		this.systemMapper = systemMapper;
	}

	/**
	 * Android客户端版本更新，验证是否有新的版本
	 * 
	 * @param version
	 *            客户端版本号
	 * @return
	 */
	@Override
	public List<Version> getVersion(int version) {
		return systemMapper.getVersion(version);
	}

	/**
	 * 记录API调用数据
	 * 
	 * @param request
	 * @param result
	 */
	@Override
	public void logApi(HttpServletRequest request, ApiResult result) {

		Apilog apilog = new Apilog();
		if (result != null) {
			apilog.setResponse(result.toString());
		}
		if (result != null && result.getResponse() != null
				&& result.getResponse().getHeader() != null) {
			apilog.setStatue(result.getResponse().getHeader().getRc() + "");
		}

		Enumeration<?> paramNames = request.getParameterNames();
		Map<String, Object> params = new HashMap<String, Object>();
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String values[] = null;
			if (!"pass".equals(paramName) && !"oldpass".equals(paramName)
					&& !"newPass".equals(paramName)) {
				values = request.getParameterValues(paramName);
			}
			// 当为文件参数时,将参数置空,以免,数据库字段长度不够
			if ("file_stream".equals(paramName)
					|| "image_ids".equals(paramName)) {
				values = null;
			}
			if (values != null && values.length != 0)
				if (values.length > 1) {
					params.put(paramName, values);
				} else {
					params.put(paramName, values[0]);
				}
		}
		apilog.setParam(JSONObject.fromObject(params).toString());
		apilog.setIp(request.getRemoteHost());
		apilog.setRequest(request.getRequestURI());
		systemMapper.logApi(apilog);
	}

	/**
	 * 记录API调用数据（分页）
	 * 
	 * @param request
	 * @param result
	 */
	@Override
	public void logApiPage(HttpServletRequest request, ApiResultPage result) {

		Apilog apilog = new Apilog();
		if (result != null) {
			apilog.setResponse(result.toString());
		}
		if (result != null && result.getResponse() != null
				&& result.getResponse().getHeader() != null) {
			apilog.setStatue(result.getResponse().getHeader().getRc() + "");
		}

		Enumeration<?> paramNames = request.getParameterNames();
		Map<String, Object> params = new HashMap<String, Object>();
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String values[] = request.getParameterValues(paramName);
			if (values != null && values.length != 0)
				if (values.length > 1) {
					params.put(paramName, values);
				} else {
					params.put(paramName, values[0]);
				}
		}
		apilog.setParam(JSONObject.fromObject(params).toString());
		apilog.setIp(request.getRemoteHost());
		apilog.setRequest(request.getRequestURI());
		systemMapper.logApi(apilog);
	}

	public boolean isUserLogin(String userid, String token) {
		boolean flag = false;
		ApiToken api = new ApiToken();
		api.setUserid(userid);
		api.setToken(token);
		int count = systemMapper.getUserCount(api);
		if (count > 0) {
			flag = true;
		}
		return flag;
	}

	public boolean isMemberLogin(String userid, String token) {
		boolean flag = false;
		ApiToken api = new ApiToken();
		api.setMemberid(userid);
		api.setToken(token);
		int count = systemMapper.getMemberCount(api);
		if (count > 0) {
			flag = true;
		}
		return flag;
	}

}